<template>
	<view class="wh100 l-box-pf-l c-bg-f" @touchmove.stop.prevent="moveStop">
		<view class="title">请在下面输入签名：</view>
		<canvas class="mycanvas" id="canvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
			@touchend="touchend"></canvas>
		<view class="footer">
			<view class="left" @click="finish">完成</view>
			<view class="right" @click="clear">清除</view>
		</view>
	</view>
</template>

<script>
	// import config from '../common/config.js'
	var x = 20;
	var y = 20;
	export default {
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合 
				agh: false, //是否签名
				obj: {},
				type: ''
			}
		},
		
		created(opt) {
			// #ifdef APP-PLUS
			   plus.screen.lockOrientation('default'); 
			// #endif

			this.ctx = uni.createCanvasContext("mycanvas", this); //创建绘图对象
			
			//设置画笔样式
			this.ctx.lineWidth = 4;
			this.ctx.lineCap = "round"
			this.ctx.lineJoin = "round"
			
			
		},
		mounted() {
			this.getDomeh()
		},
		beforeDestroy() {
			// #ifdef APP-PLUS
			   plus.screen.lockOrientation('portrait-primary'); 
			// #endif

		},
		methods: {
			//获取元素的宽高
			getDomeh() {
				const query = uni.createSelectorQuery().in(this);
				query.select('#canvas').boundingClientRect(data => {
					//填充色
					this.ctx.drawImage('/static/images/white.png', 0, 0, data.width, data.height)
				}).exec();
			},
			moveStop() {},
			//触摸开始，获取到起点
			touchstart(e) {
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};

				/* **************************************************
					#由于uni对canvas的实现有所不同，这里需要把起点存起来
				 * **************************************************/
				this.points.push(startPoint);

				//每次触摸开始，开启新的路径
				this.ctx.beginPath();
			},

			//触摸移动，获取到路径点
			touchmove(e) {
				this.agh = true;
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}

			},

			// 触摸结束，将未绘制的点清空防止对后续路径产生干扰
			touchend() {
				this.points = [];
			},

			/* ***********************************************
			#   绘制笔迹
			#	1.为保证笔迹实时显示，必须在移动的同时绘制笔迹
			#	2.为保证笔迹连续，每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
			#	3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
			************************************************ */
			draw() {
				let point1 = this.points[0]
				let point2 = this.points[1]
				this.points.shift()
				this.ctx.moveTo(point1.X, point1.Y)
				this.ctx.lineTo(point2.X, point2.Y)
				this.ctx.stroke()
				this.ctx.draw(true)
			},

			//清空画布
			clear() {
				this.agh = false;
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.ctx.clearRect(0, 0, canvasw, canvash);
						that.ctx.draw(true);
					},
				})
				this.$emit('reset', {})
			},

			//完成绘画
			finish() {
				if (!this.agh) {
					this.$Function.Toast('请签名')
					return
				}
				uni.showModal({
					title: '提示',
					content: '是否确认使用签名并提交',
					success: (res) => {
						if (res.confirm) {
							uni.canvasToTempFilePath({
								canvasId: 'mycanvas',
								success: (res) => {
									let tempFilePath = res.tempFilePath;
									uni.showLoading({
										title: '图片上传中...',
										mask: true
									});
									var promise = new Promise((resolve, reject) => {
										uni.uploadFile({
											url: config.url_config +'/blade-resource/oss/endpoint/put-file', //仅为示例，非真实的接口地址
											header: {
												"Blade-Auth": 'bearer ' +uni.getStorageSync('token')
											},
											filePath: tempFilePath,
											name: 'file',
											formData: {},
											success: (uploadFileRes) => {
												let data = JSON.parse(uploadFileRes.data)
												if (data.code == 401) {
													this.$Function.LinkTo('/pages/mine/sign','Rl')
													return
												}
												resolve(data.data.name)
											}
										});
									})
									
									promise.then(res => {
										this.$emit('sign', {img:res})
										uni.hideLoading();
										
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}
		},
	}
</script>

<style>
	.title {
		height: 50upx;
		line-height: 50upx;
		font-size: 16px;
	}

	.mycanvas {
		width: 100%;
		height: calc(100vh - 200upx);
		background-color: #FFFFFF;
	}

	.footer {
		font-size: 16px;
		height: 150upx;
		display: flex;
		justify-content: space-around;
		align-items: center;
		z-index: 9999999;
		background-color: #f9f9f9;
	}

	.left,
	.right {
		line-height: 100upx;
		height: 100upx;
		width: 250upx;
		text-align: center;
		font-weight: bold;
		color: white;
		border-radius: 5upx;
	}

	.left {
		background: #007AFF;
	}

	.right {
		background: orange;
	}
</style>
